package com.thon.controller.rest;


import javax.validation.Valid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.thon.dao.util.Page;
import com.thon.entity.project.Product;
import com.thon.service.project.ProductService;

/**
 * @file ProductRestController.java
 * @author Zemo
 * @mail zemochen@gmail.com
 * @data 2013年11月21日 下午3:10:31
 * @description: 商品相关接口
 */
@Controller
@RequestMapping(value = "/api/project")
@SuppressWarnings({ "unchecked", "rawtypes" })
public class ProductRestController {
	
	private static final Log log = LogFactory.getLog(ProductRestController.class);
	@Autowired
	private ProductService productService;
	
	
	//根据项目id添加商品
	@RequestMapping(value = "/product/add",method = RequestMethod.POST)
	public ResponseEntity<HttpEntity> addProduct(@Valid @RequestBody Product product){
		product.setAmmount(0);
		product.setRemaining(product.getAmmount());
		product.setPermissions("yes");
		productService.add(product);
		return new ResponseEntity(product,HttpStatus.CREATED);
	}

	//根据Project id 分页获取商品列表
	@RequestMapping(value="/product/list",method = RequestMethod.GET)
	public ResponseEntity<HttpEntity> apiFindProductByProjectId(@RequestParam(value="pid",required = true)int projectId,
			@RequestParam(value = "p",defaultValue = "1")int pageNo,
			@RequestParam(value = "s",defaultValue = "10")int pageSize,
			@RequestParam(value = "y",defaultValue = "0")int isShowAll){
		
		Page<Product> page = productService.findProductByproject(projectId, pageNo, pageSize,isShowAll);
		return new ResponseEntity(page, HttpStatus.OK);
	}
	

	
	//根据Product id 删除商品
	@RequestMapping(value = "/product/del",method=RequestMethod.POST)
	public ResponseEntity<HttpEntity> apiDelProduct(@RequestParam(value = "id",required = true)Integer id){
		productService.delete(id);
		return new ResponseEntity(HttpStatus.NO_CONTENT);
	}
	
	//更新product
	@RequestMapping(value = "/product/update",method=RequestMethod.POST)
	public ResponseEntity<HttpEntity> apiUpdateProduct(@Valid @RequestBody Product product){
		
		Product oldProduct = new Product();
		oldProduct = productService.getProduct(product.getId());
		oldProduct.setAmmount(product.getAmmount());
		oldProduct.setDescription(product.getDescription());
		oldProduct.setName(product.getName());
		oldProduct.setPrice(product.getPrice());
		oldProduct.setPhotoId(product.getPhotoId());
		oldProduct.setRemaining(product.getAmmount());
		//oldProduct.setLimit(product.getLimit());
		//oldProduct.setAmmount(0);
		productService.update(oldProduct);
		return new ResponseEntity(product,HttpStatus.CREATED);
	}
	//关闭商品购买权限
	@RequestMapping(value="/product/close",method=RequestMethod.POST)
	public ResponseEntity<?> closeProduct(@RequestParam(value="productId") int productId){
		Product product = productService.getProduct(productId);
		product.setPermissions("no");
		productService.update(product);
		return new ResponseEntity(true,HttpStatus.OK);
	}
	//
	//开启商品购买权限
		@RequestMapping(value="/product/open",method=RequestMethod.POST)
		public ResponseEntity<?> openProduct(@RequestParam(value="productId") int productId){
			Product product = productService.getProduct(productId);
			product.setPermissions("yes");
			productService.update(product);
			return new ResponseEntity(true,HttpStatus.OK);
		}
}
